package com.kan.bargain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.kan.bargain.feginbar.Barginfegin;
import com.kan.bargain.feignborder.FeignBargain;
import com.miao.commonn.utils.R;
import com.miao.commonn.vo.BargainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miao.commonn.utils.PageUtils;
import com.miao.commonn.utils.Query;

import com.kan.bargain.dao.BordersDao;
import com.kan.bargain.entity.BordersEntity;
import com.kan.bargain.service.BordersService;
import org.springframework.transaction.annotation.Transactional;


@Service("bordersService")
public class BordersServiceImpl extends ServiceImpl<BordersDao, BordersEntity> implements BordersService {

    @Autowired
    Barginfegin barginfegin;

    @Autowired
    FeignBargain feignBargain;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BordersEntity> page = this.page(
                new Query<BordersEntity>().getPage(params),
                new QueryWrapper<BordersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R instart(Integer bid,Integer uid) {

        R info = barginfegin.info1(bid);
        BargainVo bargain =  info.getData("bargain",new TypeReference<BargainVo>(){});



        BordersEntity bordersEntity = new BordersEntity();

        bordersEntity.setUserId(uid);
        bordersEntity.setBid(bid);
        bordersEntity.setBcount(1);
        bordersEntity.setCreateDate(new Date());
        bordersEntity.setGoodsId(bargain.getGid());
        bordersEntity.setGoodsPrice(bargain.getGprice());
        bordersEntity.setShiPrice(bargain.getGprice());
        bordersEntity.setStatus(0);
        bordersEntity.setCountorlow(bargain.getCountorlow());
        bargain.setBcount(bargain.getBcount()-1);

        feignBargain.update(bargain);


        baseMapper.insert(bordersEntity);

        return R.ok();
    }


}